/*
*js格式化数字代码
*
*value: 要格式化的数字值
*scale: 最多保留几位小数
*zeroed: 是否保留尾0
*percented: 是否转称百分比形式
*
*/
function formatNumber(value, scale, zeroed, percented){
    if (value == null)
        return percented ? '0%' : 0;
    var mr = (''+value).match(/^\d+\.?\d*$/);
    if (!mr)
        return percented ? '0%' : 0;
    mr = (percented ? (value = Number(mr[0]) * 100)+'' : mr[0]).split('.');console.info(mr);
    if (mr.length == 1)
        return (zeroed ? (mr[0] + ((function(){var r = '.';for (var i = 0; i < scale; i++) {
            r += '0';
        }return r;}()))) : mr[0]) + (percented ? '%' : '');
    var mr_l = mr[0], mr_r = mr[1];
    if (mr_r.length == scale)
        return (zeroed ? (mr_l + '.' + mr_r) : (value + '').replace(/\.*0+$/, '')) + (percented ? '%' : '');
    else if (mr_r.length < scale)
        return (zeroed ? value + ((function(){var r = ''; for (var i = 0; i < scale - mr_r.length; i++) {
            r += '0';
        }return r;})()) : (value + '').replace(/\.*0+$/, '')) + (percented ? '%' : '');
    else {
        var _s = mr_r.substr(0, scale + 1);
        _s = _s.charAt(scale) > 4 ? (Number(_s.substring(0, scale)) + 1) + '' : _s.substring(0, scale);
        if (_s.length == (scale + 1)){
            mr_l = (Number(mr_l) + Number(_s.charAt(0))) + '';
            _s = _s.substring(1);
        }
        return (zeroed ? (mr_l + '.' + _s) : (mr_l + (_s.match(/^0*$/) ? '' : ('.' + _s.replace(/0+$/, '')))))
            + (percented ? '%' : '');
    }
};





/*
*js数字千分位格式化代码
*
*value: 要格式化的数字值
*seperator: 千分位符号
*digitNum: 保留几位小数
*/

function formatNumber2(value, seperator, digitNum){

    var initV = value;
    if((value = ((value = value + "").replace(/^\s*|\s*$|,*/g, ''))).match(/^\d*\.?\d*$/) == null)
        return initV;
    value = digitNum >= 0 ? (Number(value).toFixed(digitNum) + "") : value;
    var r = [], tl = value.split(".")[0], tr = value.split(".")[1];
    tr = typeof tr != "undefined" ? tr : "";
    if (seperator != null && seperator != "") {
        while(tl.length >= 3) {
            r.push(tl.substring(tl.length - 3));
            tl = tl.substring(0, tl.length - 3);
        }
        if (tl.length > 0)
            r.push(tl);
        r.reverse();
        r = r.join(seperator);
        return tr == "" ? r : r + "." + tr;
    }
    return value;
};